package com.example.problem1;

import com.example.problem1.api.DemoFactory;
import com.example.problem1.api.IDemoService;
import com.example.problem1.api.impl.AutowireFactory;
import com.example.problem1.entity.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
public class Problem1Application implements ApplicationRunner {

	@Autowired
	private IDemoService demoService;

	public static void main(String[] args) {
		SpringApplication.run(Problem1Application.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		testXmlBean();
	}

	public void testXmlBean() {
		// 使用xml装配
		ApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:/demo-application.xml");
		// 静态方法装配
		System.out.println(Demo.createDemo());
		// 使用setter方法装配
		Demo demo1 = xmlApplicationContext.getBean("demo1", Demo.class);
		System.out.println(demo1);
		// 构造方法
		Demo demo2 = xmlApplicationContext.getBean("demo2", Demo.class);
		System.out.println(demo2);

		// 自定义factoryBean
		Demo demo4 = xmlApplicationContext.getBean("demoFactoryBean", Demo.class);
		System.out.println(demo4);

		// AutowireFactory装配
		AutowireCapableBeanFactory autowireCapableBeanFactory = xmlApplicationContext.getAutowireCapableBeanFactory();
		DemoFactory demoFactory = autowireCapableBeanFactory.createBean(AutowireFactory.class);
		System.out.println(demoFactory.createDemo());
	}
}
